package com.logistics.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.logistics.common.core.constant.Constants;
import com.logistics.system.domain.Contracts;
import com.logistics.system.domain.TransportPlans;
import com.logistics.system.domain.Waybills;
import com.logistics.system.service.IContractsService;
import com.logistics.system.service.ITransportPlansService;
import com.logistics.system.service.IWaybillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.logistics.common.log.annotation.Log;
import com.logistics.common.log.enums.BusinessType;
import com.logistics.common.security.annotation.RequiresPermissions;
import com.logistics.system.domain.PayTrade;
import com.logistics.system.service.IPayTradeService;
import com.logistics.common.core.web.controller.BaseController;
import com.logistics.common.core.web.domain.AjaxResult;
import com.logistics.common.core.utils.poi.ExcelUtil;
import com.logistics.common.core.web.page.TableDataInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * 支付订单Controller
 * 
 * @author logistics
 * @date 2024-06-23
 */
@RestController
@RequestMapping("/trade")
public class PayTradeController extends BaseController
{
    @Autowired
    private IPayTradeService payTradeService;
    @Autowired
    private ITransportPlansService transportPlansService;
    @Autowired
    private IWaybillsService waybillsService;
    @Autowired
    private IContractsService contractsService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询支付订单列表
     */
    @RequiresPermissions("logistics:trade:list")
    @GetMapping("/list")
    public TableDataInfo list(PayTrade payTrade)
    {
        startPage();
        List<PayTrade> list = payTradeService.selectPayTradeList(payTrade);
        return getDataTable(list);
    }

    /**
     * 导出支付订单列表
     */
    @RequiresPermissions("logistics:trade:export")
    @Log(title = "支付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayTrade payTrade)
    {
        List<PayTrade> list = payTradeService.selectPayTradeList(payTrade);
        ExcelUtil<PayTrade> util = new ExcelUtil<PayTrade>(PayTrade.class);
        util.exportExcel(response, list, "支付订单数据");
    }

    /**
     * 获取支付订单详细信息
     */
    @RequiresPermissions("logistics:trade:query")
    @GetMapping(value = "/{tradeId}")
    public AjaxResult getInfo(@PathVariable("tradeId") Long tradeId)
    {
        return success(payTradeService.selectPayTradeByTradeId(tradeId));
    }

    /**
     * 新增支付订单
     */
    @RequiresPermissions("logistics:trade:add")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayTrade payTrade)
    {
//        Waybills waybills = waybillsService.selectWaybillsByWaybillId((payTrade.getWaybillId()));
//        if(waybills.getWaybillStatus().equals(Constants.WAYBILL_STATUS_3)){
//            return AjaxResult.error("运单状态不是已完成，无法创建订单");
//        }
//        waybills.setWaybillStatus(Constants.WAYBILL_STATUS_3);
//        waybillsService.updateWaybills(waybills);
//
//
//        Long contractId=transportPlansService.selectContractIdByWaybillId((payTrade.getWaybillId()));
//        Contracts contracts = contractsService.selectContractsByContractId(contractId);
//        contracts.setContractStatus(Constants.CONTRACTS_STATUS_3);
//        contractsService.updateContracts(contracts);
//
//        Long planId=waybillsService.selectTransportPlanIdByWaybillId((payTrade.getWaybillId()));
//        System.out.println(planId);
//        TransportPlans transportPlans = transportPlansService.selectTransportPlansByPlanId(planId);
//        transportPlans.setPlanStatus(Constants.PLAN_STATUS_2);
//        transportPlansService.updateTransportPlans(transportPlans);

        return toAjax(payTradeService.insertPayTrade(payTrade));
    }

    /**
     * 修改支付订单
     */
    @RequiresPermissions("logistics:trade:edit")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayTrade payTrade)
    {
        return toAjax(payTradeService.updatePayTrade(payTrade));
    }

    /**
     * 删除支付订单
     */
    @RequiresPermissions("logistics:trade:remove")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tradeIds}")
    public AjaxResult remove(@PathVariable Long[] tradeIds)
    {
        return toAjax(payTradeService.deletePayTradeByTradeIds(tradeIds));
    }

    /**
     * 支付完成
     */
    @RequiresPermissions("system:trade:payTradeAdd")

    @GetMapping("/payTradeAdd/{tradeId}")
    public AjaxResult payTradeAdd(@PathVariable Long tradeId) {

        // 队列名称
        String queueName = "basic.queue1";
        // 消息

//        PayTrade payTrade=payTradeService.selectPayTradeByTradeId(tradeId);
//        Long payerId=payTrade.getPayer();
//        Long payeeId=payTrade.getPayee();
//        String trade_status=payTrade.getTradeStatus();


        Long message = tradeId;
        // 发送消息
        rabbitTemplate.convertAndSend(queueName,message);
        System.out.println("11111");




//        return toAjax();
        return null;
    }
}
